package com.help10000.rms.mvp.presenter

import com.amir.common.api.CustomObserver
import com.amir.common.beans.AddressEntity
import com.amir.common.beans.BaseResult
import com.amir.common.mvp.model.Model
import com.amir.common.mvp.presenter.BasePresenter
import com.amir.common.utils.Constant
import com.amir.common.utils.SPUtils
import com.amir.common.utils.ToastUtil
import com.help10000.rms.mvp.view.AddressView

class AddressListPresenter(view: AddressView) : BasePresenter<AddressView>(view) {


    /**
     * 获取地址列表
     */
    fun getAddressList() {
        val AccessToken = SPUtils.getString(Constant.AccessToken)
        val updateNickname = Model.getServer().AddressList(AccessToken)
        Model.getObservable(updateNickname, object : CustomObserver<AddressEntity>(mvpView) {

            override fun success(t: AddressEntity) {
                mvpView?.onDataSuccess(t)
            }
        })
    }

    /**
     * 设置为默认
     */
    fun setDefault(AddrId: Int) {
        val AccessToken = SPUtils.getString(Constant.AccessToken)
        val updateNickname = Model.getServer().setAddressDefault(AccessToken, AddrId)
        Model.getObservable(updateNickname, object : CustomObserver<BaseResult>(mvpView) {

            override fun success(t: BaseResult) {
                ToastUtil.show("设置成功")
                mvpView?.success()
            }
        })
    }

    /**
     * 删除地址
     */
    fun deleteAddress(AddrId: Int) {
        val AccessToken = SPUtils.getString(Constant.AccessToken)
        val deletAddress = Model.getServer().deleteAddress(AccessToken, AddrId)
        Model.getObservable(deletAddress, object : CustomObserver<BaseResult>(mvpView) {

            override fun success(t: BaseResult) {
                ToastUtil.show("删除成功")
                mvpView?.success()
            }
        })
    }


}